#!/usr/bin/python
# -*- coding: utf-8 -*-

#异常处理
# try:
#     open('test.txt', 'r', encoding='UTF-8')
# except:
#     open('test.txt', 'w', encoding='UTF-8')
#

# set_01 = {1, 2, 3, 4}
# set_01.difference()#取两个集合的差集
# set_01.intersection()#取两个集合的交集
# set_01.union()#取两个集合的并集
#捕获指定异常
# try:
#     print(num)
# except NameError:
#     print('..............')

#捕获多个指定异常
# try:
#     print(1 / 0)
# except (NameError, ZeroDivisionError):
#     print('..............................')

#捕获异常描述信息
# try:
#     print(1/ 0)
# except (NameError, ZeroDivisionError) as result:
#     print(result)

#捕获所有异常：
# try:
#     print(num)
#     print(1/ 0)
# except Exception as result:
#     print(result)

#异常的else
#else写在except后面，表示没有异常要执行的代码
# try:
#     print(1)
# except:
#     print('...........')
# else:
#     print('没有异常')


#finally
#finally是有没有异常都要执行的代码
# try:
#     open('test.txt', 'r', encoding='UTF-8')
# except:
#     open('test.txt', 'w', encoding='UTF-8')
# else:
#     print('正常执行')
# finally:
#     print('finally')
#

#异常的传递
#只读方式打开test.txt文件
# import time
#
# try:
#     file_01 = open('test.txt', 'r', encoding='UTF-8')
#     try:
#         while True:
#             if len(file_01.readlines()) == 0:
#                 break
#             time.sleep(2)
#             print(file_01.readlines())
#     except:
#             # 如果在读取⽂件的过程中，产⽣了异常，那么就会捕获到
# #           # ⽐如 按下了 ctrl+c
#         print('意外终止了')
#     finally:
#         file_01.close()
#         print('关闭文件')
# except:
#     print('没有这个文件')


#自定义异常
#自定义异常类，集成exception
# class ShotInputError(Exception):
#     def __init__(self, length, min_len):
#         self.length = length
#         self.min_len = min_len
#
#     #设置抛出异常的描述信息
#     def __str__(self):
#         return ('你输入的长度是{0}, 不能少于{1}个字符'.format(self.length, self.min_len))
#
#
# def main():
#     try:
#         con = input('请输入密码:')
#         if len(con) < 3:
#             raise ShotInputError(len(con), 3)
#     except Exception as result:
#         print(result)
#     else:
#         print('密码输入完成。。。。。。')
#
# main()

# 异常的传递
# try:
#     print('代码.................')
#     try:
#         # print(n)
#         1/0
#     except NameError as msg:
#         print(f'变量未定义')
# except Exception as msg:
#     print(f'异常信息{msg}')
#     raise

